// 日期封装
import Foundation
extension Date {

    // 计算当前年份 数字形式
    static  func  getCurrentYearNum() ->  Int  {
        let yearNum =  Calendar.current.dateComponents(in: TimeZone.current, from: Date()).year!
        return yearNum
    }
    
    // 根据日期获取年份
    static  func  getYearByDate(date:Date) ->  String  {
        let yearNum =  Calendar.current.dateComponents(in: TimeZone.current, from: date).year!
        return String(yearNum)
    }
    
    
  
    
    // 计算当前月份 数字形式
    static func getCurrentMonthNum() -> Int {
        let monthNum =   Calendar.current.dateComponents(in: TimeZone.current, from:  Date()).month!
        return monthNum
    }
    
    // 根据日期得到月份
     static  func getMonthByDate(date:Date) -> String {
        let monthNum =   Calendar.current.dateComponents(in: TimeZone.current, from: date).month!
        return   String(monthNum)
    }
   
    
    //计算当前日期 文字形式
    func getCurrentDay() -> Int {
       
        return  Calendar.current.dateComponents(in: TimeZone.current, from: Date()).day!
    }
    
    // 获取年月日 文字形式
    func getDate() -> String {
        // 获取当前日期
        let currentDate = Date()

        // 创建一个DateFormatter对象
        let dateFormatter = DateFormatter()

        // 设置日期格式为"yyyy-MM"
        dateFormatter.dateFormat = "yyyy-MM-dd"

        // 使用DateFormatter将日期转换为字符串
        let currentYearMonth = dateFormatter.string(from: currentDate)
        return currentYearMonth
    }
    
    //计算当前月有多少天
    static  func getNumDays(year:String,month: String) -> Int {
        let dateComponents = DateComponents(year: Int(year), month: Int(month))
        let calendar = Calendar.current
        let date = calendar.date(from: dateComponents)!
        let range = calendar.range(of: .day, in: .month , for: date)!
        let numDays = range.count
        return numDays
    }
    
    //本月开始日期
    static func startOfCurrentMonth() -> Date {
        let date = Date()
        let calendar = NSCalendar.current
        let components = calendar.dateComponents(
            Set<Calendar.Component>([.year, .month]), from: date)
        let startOfMonth = calendar.date(from: components)!
        return startOfMonth
    }
    
    //根据月份获取当月第一天是星期几
    static func getWeekDayByFirstDayAndMonth(year:String,month:String) -> Int {
        let dateStr = year + "-" + month
        let calendar = NSCalendar.current
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: " zh_CN")
        formatter.dateFormat = "yyyy-MM"
        let date = formatter.date(from: dateStr)

        let components = calendar.dateComponents(
            Set<Calendar.Component>([.year, .month]), from: date!)
        let startOfMonth = calendar.date(from: components)!
        let weekDay = Calendar.current.dateComponents(in: TimeZone.current, from: startOfMonth).weekday! - 1
        return weekDay
    }
    
    // 计算当月第一天是星期几
    static func getWeekDayByFirstDay() -> Int {
        let firstDate = startOfCurrentMonth()
        let weekDay =   Calendar.current.dateComponents(in: TimeZone.current, from: firstDate).weekday! - 1
        return weekDay
    }
    
  
    
    
    // 获取本周所有日期 星期天 - 星期六
    static func getCurrentWeekDays() -> [Int] {
          let calendar = Calendar.current
          let today = Date()
          guard let weekInterval = calendar.dateInterval(of: .weekOfYear, for: today) else { return [] }
          
          var days: [Int] = []
          calendar.enumerateDates(startingAfter: weekInterval.start, matching: DateComponents(hour: 0, minute: 0, second: 0), matchingPolicy: .nextTime) { date, _, stop in
              if let date = date {
                  if date <= weekInterval.end {
                      let components = calendar.dateComponents([.day], from: date)
                      if let day = components.day {
                          days.append(day-1)
                      }
                  } else {
                      stop = true
                  }
              }
          }
          
          return days
      }
    
 
    // 计算下个月的月份的日期
    func nextMonth() -> Date {
           let components = Calendar.current.dateComponents([.year, .month], from: self)
           var nextMonthComponents = DateComponents()
           nextMonthComponents.year = components.year!
           nextMonthComponents.month = components.month! + 1
           if nextMonthComponents.month! > 12 {
               nextMonthComponents.month = 1
               nextMonthComponents.year = components.year! + 1
           }
           return Calendar.current.date(from: nextMonthComponents)!
       }
    
    // 计算上个月的月份的日期
    func previousMonth() -> Date {
            let components = Calendar.current.dateComponents([.year, .month], from: self)
            var previousMonthComponents = DateComponents()
            previousMonthComponents.year = components.year!
            previousMonthComponents.month = components.month! - 1
            if previousMonthComponents.month! < 1 {
                previousMonthComponents.month = 12
                previousMonthComponents.year = components.year! - 1
            }
            return Calendar.current.date(from: previousMonthComponents)!
        }
}
